package cn.com.ksplatform.base.hibernate.dialect;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.com.ksplatform.base.page.PageInfoModel;


@Component("dialects.mysql")
public class MySQLDialect implements IDialect {

	@Override
	public String getPageSql(String sql, PageInfoModel page) {
		// 设置分页参数
		int pageSize = page.getPageSize();
		int pageNo = page.getPage();
		StringBuffer sb = new StringBuffer(sql);
		if (StringUtils.hasText(page.getSortName())) {
			sb.append(" order by ").append(page.getSortName()).append(" ").append(page.getSortOrder());
		}
		sb.append(" limit ").append(pageSize * (pageNo - 1)).append(",").append(pageSize);
		return sb.toString();
	}

	@Override
	public String getCountSql(String sql) {
		String countSql = "select count(*) from ("+sql+") t1";
		return countSql;
	}

}
